package com.gw.quotes.activity;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Shader;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.gw.quotes.R;
import com.gw.quotes.util.BitmapTool;
import com.gw.quotes.view.DrawableCenterCheckedTextView;

public class OrderEvaActivity extends BaseActivity implements
		OnRatingBarChangeListener, OnClickListener {

	private String eva_class = "好评";
//	private Order order;
	private RatingBar ratingBarArr[] = new RatingBar[3];
	private float ratingArr[] = { 0.0f, 0.0f, 0.0f };
	private int textViewId[] = { R.id.good, R.id.general, R.id.poor };
	private DrawableCenterCheckedTextView textView[] = new DrawableCenterCheckedTextView[3];

	@ViewInject(id = R.id.back_img, click = "back")
	private ImageView back_img;
	@ViewInject(id = R.id.eva_button, click = "eva_btn")
	private Button eva_btn;
	@ViewInject(id = R.id.order_goods_img)
	private ImageView goods_img;
	@ViewInject(id = R.id.order_goods_name)
	private TextView goods_name;
	@ViewInject(id = R.id.good)
	private DrawableCenterCheckedTextView good;
	@ViewInject(id = R.id.general)
	private DrawableCenterCheckedTextView general;
	@ViewInject(id = R.id.poor)
	private DrawableCenterCheckedTextView poor;
	@ViewInject(id = R.id.order_ratingbar)
	private RatingBar ratingBar1;
	@ViewInject(id = R.id.order_ratingbar2)
	private RatingBar ratingBar2;
	@ViewInject(id = R.id.order_ratingbar3)
	private RatingBar ratingBar3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_eva);
		getData();
		initView();
	}

	private void initView() {
		good.setChecked(true);
//		goods_img.setImageResource(order.getGoodsImg());
//		goods_name.setText(order.getGoodsName());
		good.setOnClickListener(this);
		general.setOnClickListener(this);
		poor.setOnClickListener(this);
		ratingBar1.setOnRatingBarChangeListener(this);
		ratingBar2.setOnRatingBarChangeListener(this);
		ratingBar3.setOnRatingBarChangeListener(this);
		textView[0] = good;
		textView[1] = general;
		textView[2] = poor;
		ratingBarArr[0] = ratingBar1;
		ratingBarArr[1] = ratingBar2;
		ratingBarArr[2] = ratingBar3;
	}

	private void getData() {
		Intent intent = getIntent();
//		order = (Order) intent.getSerializableExtra("orderInfo");
	}

	// 提交评价
	public void eva_btn(View v) {
		for (int i = 0; i < ratingArr.length; i++) {
			if (ratingArr[i] == 0.0) {
				Toast.makeText(OrderEvaActivity.this, "还没评分哦,亲", 1).show();
				return;
			}
		}
		Toast.makeText(
				OrderEvaActivity.this,
				eva_class + "\n" + ratingArr[0] + " " + ratingArr[1] + " "
						+ ratingArr[2], 1).show();
	}

	public void back(View v) {
		finish();
	}

	private Drawable buildRatingBarDrawables(Bitmap[] images) {
		final int[] requiredIds = { android.R.id.background,
				android.R.id.secondaryProgress, android.R.id.progress };
		final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
		Drawable[] pieces = new Drawable[3];
		for (int i = 0; i < 3; i++) {
			ShapeDrawable sd = new ShapeDrawable(new RoundRectShape(
					roundedCorners, null, null));
			BitmapShader bitmapShader = new BitmapShader(images[i],
					Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
			sd.getPaint().setShader(bitmapShader);
			ClipDrawable cd = new ClipDrawable(sd, Gravity.LEFT,
					ClipDrawable.HORIZONTAL);
			if (i == 0) {
				pieces[i] = sd;
			} else {
				pieces[i] = cd;
			}
		}
		LayerDrawable ld = new LayerDrawable(pieces);
		for (int i = 0; i < 3; i++) {
			ld.setId(i, requiredIds[i]);
		}
		return ld;
	}

	@Override
	public void onRatingChanged(RatingBar ratingBar, float rating,
			boolean fromUser) {
		for (int i = 0; i < ratingArr.length; i++) {
			if (ratingBar == ratingBarArr[i]) {
				Bitmap a[] = {
						BitmapTool.changeToBitmap(getResources(),
								R.drawable.star_empty),
						BitmapTool.changeToBitmap(getResources(),
								R.drawable.star_empty),
						BitmapTool.changeToBitmap(getResources(),
								R.drawable.star_orange) };
				Bitmap b[] = {
						BitmapTool.changeToBitmap(getResources(),
								R.drawable.star_empty),
						BitmapTool.changeToBitmap(getResources(),
								R.drawable.star_empty),
						BitmapTool.changeToBitmap(getResources(),
								R.drawable.star_gray) };
				if (rating >= 3.0) {
					ratingBar.setProgressDrawable(buildRatingBarDrawables(a));
				} else {
					ratingBar.setProgressDrawable(buildRatingBarDrawables(b));
				}
				ratingArr[i] = rating;
			}
		}
	}

	@Override
	public void onClick(View v) {
		for (int i = 0; i < textViewId.length; i++) {
			if (v.getId() == textViewId[i]) {
				if (good.isChecked()) {
					good.toggle();
				}
				if (general.isChecked()) {
					general.toggle();
				}
				if (poor.isChecked()) {
					poor.toggle();
				}
				textView[i].setChecked(true);
				eva_class = textView[i].getText().toString();
			}
		}

	}
}
